const request=require('../../utils/request.js');//获取封装的请求函数
const globleurl = require('../../utils/serverconfig.js').url;
const formattime=require('../../utils/formattime').formatTime;
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageURL:globleurl+'/釜山行.jpg',
    day_id:0,
    moviename:'',
    movietimes:'',
    totalprice:0,
    cinemanm:'',
    seatlist:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let seatlist=JSON.parse(options.seatFinish)
    let price=options.price;
    let cinemanm=options.cinemanm;
    let hall_id=options.hall_id;
    this.setData({
      day_id:options.day_id,
      moviename:options.moviename,
      movietimes:options.movietimes,
      price:options.price,
      index:options.index,
      seatlist,
      movie_id:options.movie_id,
      totalprice:Number(price)*seatlist.length*100,
      cinemanm,
      hall_id
    });
    wx.setNavigationBarTitle({
      title: `${cinemanm}`
    })

    this.getmovieimg();
  },


  async getmovieimg(){
    let movie_id=this.data.movie_id;
    let url= await request.myrequest('/miniapp/movie',{movie_id});
    url=url.data[0].img_url;
    url=globleurl+url;
    this.setData({
      imageURL:url
    });
  },

  //产生随机订单号
  random_No:function random_No(j) {
    var random_no = "";
    for (var i = 0; i < j; i++) //j位随机数，用以加在时间戳后面。
    {
        random_no += Math.floor(Math.random() * 10);
    }
    random_no = new Date().getTime() + random_no;
    return random_no;
},



//判断数据库中有没有座位信息，有更新，没有就插入更新
async confirmseat(){
  let seatlist=this.data.seatlist;
  for(let i=0;i<seatlist.length;i++){
      let re=await request.myrequest('/miniapp/confirmseat',{row:seatlist[i].row,col:seatlist[i].col,hall_id:this.data.hall_id});
    if(re.data.length==0){
      await request.myrequest('/miniapp/noseatinfo',{row:seatlist[i].row,col:seatlist[i].col,hall_id:this.data.hall_id});
    }else{
      await request.myrequest('/miniapp/updateseat',{row:seatlist[i].row,col:seatlist[i].col,hall_id:this.data.hall_id});
    }
  }
},

  onSubmit(e){
    let _this=this;
 
    if(app.globalData.openid){
      // 用户已经授权登录
      //插入订单
      async function submitorder(userid){
        let order_id=_this.random_No(1);
        let order_uid=userid;
        let day_id=_this.data.day_id;
        let totalprice=_this.data.totalprice/100;
        let creat_time=new Date();
        creat_time=formattime(creat_time);
        let seatlist=_this.data.seatlist;
        let describe='';
        for(let i=0;i<seatlist.length;i++)
        {
            describe=describe+`${seatlist[i].row}排${seatlist[i].col}列 `;      
        };
        await request.myrequest('/miniapp/ordermovie',{order_id,order_uid,day_id,totalprice,creat_time,describe});
      wx.redirectTo({
        url:`./successbuy/succesbuy?order_id=${order_id}`
      })
      } ;
       let userid;
      async function getuserid(){
            await request.myrequest('/miniapp/requestuserid',{openid:app.globalData.openid},'post').then(value1=>{
              userid=value1.data[0].user_id;
              _this.setData({
              userid:userid,
             nickName:app.globalData.userInfo.nickName
            });
            });
           submitorder(userid);
           _this.confirmseat();
        };
        getuserid();
    }else{
      //用户没有登陆授权
      wx.showToast({
        title: '未登录,请登录！',
        icon:'none'
      });
      setTimeout(()=>{
          wx.switchTab({
            url: '../individual/individual'
          })
      },3000);
    }

  }

})